﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PowerView.Page
{
    public partial class UFCS_Form : Form
    {
        const byte UFCS_Protocol = 0x20;
        byte[] data_buf = new byte[128];

        float[,] Output_Cap = new float[8, 6]; // Volt_Min, Volt_Max, Curr_Min, Curr_Max, Volt_Step, Curr_Step
        public UFCS_Form()
        {
            InitializeComponent();
        }

        public void CMD_Proc(byte[] cmd)
        {
            switch (cmd[3])
            {
                case 0x00: // ShackHand
                    break;
                case 0x01: // Get Output Cap
                    int Output_Cap_Num = cmd[5];

                    if (Output_Cap_Num == 0 || Output_Cap_Num % 8 != 0)
                    {
                        MiddleWare.log_tBox.AppendText("Output Cap 长度不对！！\r\n");
                        return;
                    }
                    Output_Cap_Num = Output_Cap_Num / 8;
                    Output_listBox.Items.Clear();
                    for (int i = 0; i < Output_Cap_Num; i++)
                    {
                        Output_Cap[i, 0] = (cmd[i * 8 + 6 + 3] * 256 + cmd[i * 8 + 6 + 4]) * 10; // Volt Min
                        Output_Cap[i, 1] = (cmd[i * 8 + 6 + 1] * 256 + cmd[i * 8 + 6 + 2]) * 10; // Volt Max
                        Output_Cap[i, 2] =  cmd[i * 8 + 6 + 7] * 10; // Curr Min
                        Output_Cap[i, 3] = (cmd[i * 8 + 6 + 5] * 256 + cmd[i * 8 + 6 + 6]) * 10; // Curr Max
                        Output_Cap[i, 4] = ((cmd[i * 8 + 6 + 0] & 0x01) + 1) * 10; // Volt Step
                        Output_Cap[i, 5] = (((cmd[i * 8 + 6 + 0] & 0x0E) >> 1) +1 ) * 10; // Curr Step

                        Output_listBox.Items.Add((i+1).ToString()+": " +
                            (Output_Cap[i, 0] / 1000).ToString() + "-" + (Output_Cap[i, 1] / 1000).ToString() + "V, " +
                            (Output_Cap[i, 2] / 1000).ToString() + "-" + (Output_Cap[i, 3] / 1000).ToString() + "A, " +
                            Output_Cap[i, 4] .ToString() + "mV, " + Output_Cap[i, 5].ToString() + "mA");

                        Console.WriteLine(Output_Cap[i, 0].ToString() + ", " + Output_Cap[i, 1].ToString() + ", " + Output_Cap[i, 2].ToString() + ", " +
                            Output_Cap[i, 3].ToString() + ", " + Output_Cap[i, 4].ToString() + ", " + Output_Cap[i, 5].ToString());
                    }
                    break;

                case 0x03:
                    if (cmd[5] < 8)
                    {
                        MiddleWare.log_tBox.AppendText("Get Source Info Fail!!!！！\r\n");
                        return;
                    }

                    Temp_Info_textBox.Text = ((cmd[6 + 2]) - 50).ToString();
                    Volt_Info_textBox.Text = ((float)(cmd[6 + 4] * 256 + cmd[6 + 5]) / 100).ToString();
                    Curr_Info_textBox.Text = ((float)(cmd[6 + 6] * 256 + cmd[6 + 7]) / 100).ToString();
                    break;
            }
        }
        private void ShakeHand_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, UFCS_Protocol, 0x00, null, 0);
        }

        private void Get_Output_Cap_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, UFCS_Protocol, 0x01, null, 0);
        }

        private void Set_Output_Btn_Click(object sender, EventArgs e)
        {
            byte[] _data = new byte[5];
            UInt32 volt = 0, curr = 0;
            _data[0] = (byte)(Output_listBox.SelectedIndex + 1);
            volt = (UInt32)(double.Parse(Volt_textBox.Text) * 1000);
            curr = (UInt32)(double.Parse(Curr_textBox.Text) * 1000);
            _data[1] = (byte)(volt >> 8);
            _data[2] = (byte)(volt & 0xFF);
            _data[3] = (byte)(curr >> 8);
            _data[4] = (byte)(curr & 0xFF);

            MiddleWare.Write_CMD(0xff, UFCS_Protocol, 0x02, _data, 5);
        }

        private void Volt_textBox_TextChanged(object sender, EventArgs e)
        {
            UInt32 volt = 0;
            float volt_s = 0;
            if (float.TryParse(Volt_textBox.Text, out volt_s) == false) return;

            volt = (UInt32)(volt_s * 1000);
            
            for (int i = 0; i < Output_listBox.Items.Count; i++)
            {
                if (Output_Cap[i, 0] <= volt && volt <= Output_Cap[i, 1])
                {
                    Output_listBox.SelectedIndex = i;
                    return;
                }
            }
        }

        private void Only_Number_textBox_KeyPress(object sender, KeyPressEventArgs e)
        { 
            e.Handled = false;
            if (Char.IsDigit(e.KeyChar)) return;
            if (e.KeyChar == '\b' || e.KeyChar == '.') return;
            e.Handled = true;
        }

        private void Exit_UFCS_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, UFCS_Protocol, 0x0f, null, 0);
        }

        private void Get_Source_Info_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, UFCS_Protocol, 0x03, null, 0);
        }

        private void Volt_textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Set_Output_Btn_Click(null, null);
            }
        }

        private void Output_listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Volt_textBox.Text = (Output_Cap[Output_listBox.SelectedIndex, 1] / 1000).ToString();
            Curr_textBox.Text = (Output_Cap[Output_listBox.SelectedIndex, 3] / 1000).ToString();
        }

        private void Output_listBox_DoubleClick(object sender, EventArgs e)
        {
            Set_Output_Btn_Click(null, null);
        }

        private void Get_Source_Info_Timer_Tick(object sender, EventArgs e)
        {
            Get_Source_Info_Btn_Click(null, null);
        }

        private void Get_Source_Info_checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if(Get_Source_Info_checkBox.Checked) Get_Source_Info_Timer.Enabled = true;
            else Get_Source_Info_Timer.Enabled= false;
        }
    }
}
